
import ac = module("utils/commons/array-collection");
import ut = module("utils/utils");
import aa = module("main/org/sdmx/model/v2/reporting/dataset/AttributeValue");
import aar = module("main/org/sdmx/model/v2/reporting/dataset/AttachableArtefact");

/**
	* A collection of attribute values. It extends the AS3 ArrayCollection
	* and simply restrict the items type to AttributeValue.
	* 
	* @author Xavier Sosnovsky, Anthony Morel
	* 
	* @see AttributeValue
	*/ 
export class AttributeValuesCollection extends ac.ArrayCollection<aa.AttributeValue> {

	/*==============================Fields================================*/
		
	private static ERROR_MSG:string = "Only attribute values are " + 
			"allowed in an attribute values collection. Got: ";

				
	/*===========================Constructor==============================*/
				
	constructor(source=null) {
		super(source);
	}
    
    /*==========================Public methods============================*/
    
    /**
        * @private
        */
    public addItemAt(item: aa.AttributeValue, index:number):ac.ArrayCollection<aa.AttributeValue> {
		if (!(item instanceof aa.AttributeValue)) {
			throw new Error(AttributeValuesCollection.ERROR_MSG + 
					ut.Utils.getQualifiedClassName(item) + ".");
		}
		return super.addItemAt(item, index);
	}
    
    /**
        * @private
        */
    public setItemAt(item: aa.AttributeValue, index: number): ac.ArrayCollection<aa.AttributeValue> {
        if (!(item instanceof aa.AttributeValue)) {
			throw new Error(AttributeValuesCollection.ERROR_MSG + 
                ut.Utils.getQualifiedClassName(item) + ".");
		}
		return super.setItemAt(item, index);
	}
		
	/**
		* Returns the attribute values attached to the supplied artefact 
		* @param item The artefact to which the attributes must be attached
		* @return The attribute values attached to the supplied artefact
		*/
	public getAttributes(
		item:aar.AttachableArtefact):AttributeValuesCollection {
		var collection:AttributeValuesCollection 
            = new AttributeValuesCollection(),
            items = this.getItems();
        for (var i = 0; i < items.length; i++) {

            if (items[i].getAttachesTo() === item) {
                collection.addItem(items[i]);
            }
        }

		return collection;
	}
}